package com.jielihaofeng.atguigu.nio;

import java.nio.ByteBuffer;

/**
 * @author Johnnie Wind
 * @description 可以将一个普通 Buffer 转成只读 Buffer
 * @date 2021/11/25 14:07
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(64);

        for (int i = 0; i < 64; i++) {
            buffer.put((byte) i);
        }

        // 读取
        buffer.flip();

        // 只读ByteBuffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        // class java.nio.HeapByteBufferR
        System.out.println(readOnlyBuffer.getClass());

        // 读取
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }

        // 再放放不了
        readOnlyBuffer.put((byte) 100);
    }
}
